package com.gljx.web.service.impl;

import com.gljx.util.GetFid;
import com.gljx.util.TimeFormat;
import com.gljx.web.Entity.Student;
import com.gljx.web.Entity.SubjectThreeResult;
import com.gljx.web.Entity.SubjectThreeResultentry;
import com.gljx.web.Entity.User;
import com.gljx.web.dao.CommonDao;
import com.gljx.web.dao.StudentMapper;
import com.gljx.web.dao.SubjectThreeResultMapper;
import com.gljx.web.dao.SubjectThreeResultentryMapper;
import com.gljx.web.service.CustomLogService;
import com.gljx.web.service.SubjectThreeResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SubjectThreeResultServiceImpl extends CommonDao implements SubjectThreeResultService {
	
	@Autowired
	private SubjectThreeResultMapper subjectThreeResultMapper;
	
	@Autowired
	private SubjectThreeResultentryMapper subjectThreeResultentryMapper;
	
	@Autowired
	private StudentMapper studentMapper;
	@Autowired
	private CustomLogService customLogService;
	
	@Override
	public Map<String, Object> selectPageCount(String psql) {
		// TODO Auto-generated method stub
		Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("Psql", psql);
        paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
        this.getSqlSession().select("com.gljx.web.dao.SubjectThreeResultMapper.pagecount", paramMap, null);
        return paramMap;
	}

	@Override
	public Map<String, Object> selectPage(int pindex, String psql, int psize) {
		// TODO Auto-generated method stub
		Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("Pindex", pindex);
        paramMap.put("Psql", psql);
        paramMap.put("Psize", psize);
        paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
        paramMap.put("v_cur", oracle.jdbc.OracleTypes.CURSOR);
        this.getSqlSession().select("com.gljx.web.dao.SubjectThreeResultMapper.pagemap", paramMap, null);
        return paramMap;
	}

	@Override
	public String selectFnumber(String companyId) {
		// TODO Auto-generated method stub
		return subjectThreeResultMapper.selectFnumber(companyId);
	}

	@Override
	public int addSubjectThreeResult(User user, SubjectThreeResult subjectThreeResult, String[] stuid, String[] stufseq,
									 String[] stucfzkzmbh, String[] stufnumber, String[] stuname, String[] stucfidcardno, String[] stucfsex,
									 String[] stucfsjhm, String[] stucflxdh, String[] stucfslzjcxdh, String[] stucfclazzName,
									 String[] stucfamount, String[] stucfreceived, String[] stucfarrearage, String[] stucffavorable,
									 String[] stucfvip, String[] stucflocal, String[] stucfexamresult, String[] stucfremark) {
		// TODO Auto-generated method stub
		int row1=0;
		int row2=0;
		//首先先添加父类
		row1= subjectThreeResultMapper.insertSelective(subjectThreeResult);
		// 添加子类
		for (int i = 0; i < stuid.length; i++) {
			SubjectThreeResultentry subjectThreeResultentry = new SubjectThreeResultentry();
			subjectThreeResultentry.setFseq(Long.valueOf(stufseq[i]));
			subjectThreeResultentry.setCfzkzmbh(stucfzkzmbh[i]);
			subjectThreeResultentry.setFid(GetFid.UUIDIntoFid());
			subjectThreeResultentry.setFparentid(subjectThreeResult.getFid());
			subjectThreeResultentry.setCfstudentid(stuid[i]);
			subjectThreeResultentry.setCfstudentno(stufnumber[i]);
			subjectThreeResultentry.setStudentName(stuname[i]);
			subjectThreeResultentry.setCfidcardno(stucfidcardno[i]);
			subjectThreeResultentry.setCfsex(Long.valueOf(stucfsex[i]));
			subjectThreeResultentry.setCfmobile(stucfsjhm[i]);
			subjectThreeResultentry.setCfphone(stucflxdh[i]);
			subjectThreeResultentry.setCfsqzjcx(stucfslzjcxdh[i]);
			subjectThreeResultentry.setCfclazz(stucfclazzName[i]);
			subjectThreeResultentry.setCfreceivable(new BigDecimal(stucfamount[i]));
			subjectThreeResultentry.setCfreceived(new BigDecimal(stucfreceived[i]));
			subjectThreeResultentry.setCfarrearage(new BigDecimal(stucfarrearage[i]));
			subjectThreeResultentry.setCffavorable(new BigDecimal(stucffavorable[i]));
			subjectThreeResultentry.setCfvip(Long.valueOf(stucfvip[i]));
			subjectThreeResultentry.setCfbdyd(Long.valueOf(stucflocal[i]));
			subjectThreeResultentry.setCfexamresult(Long.valueOf(stucfexamresult[i]));
			subjectThreeResultentry.setCfremark(stucfremark[i]);
			row2+=subjectThreeResultentryMapper.insertSelective(subjectThreeResultentry);
			//添加学员服务日志
			customLogService.addLog("考试管理", "新增了学员:"+subjectThreeResultentry.getCfstudentno()+subjectThreeResultentry.getStudentName()
					+",科目三考试结果登记!申请车型:"+subjectThreeResultentry.getCfsqzjcx()+",考试日期:"+subjectThreeResult.getFbizdate()+",单据编号:"+subjectThreeResult.getFnumber(), subjectThreeResultentry.getCfstudentid(), user.getFid());
		}
		if(row1 >0 && row2>0){
			return 1;
		}
		return 0;
	}

	@Override
	public int deleteSubjectThreeResult(String fid) {
		// TODO Auto-generated method stub
		return subjectThreeResultMapper.deleteByPrimaryKey(fid);
	}

	@Override
	public SubjectThreeResult selectSubjectThreeResultByFid(String fid) {
		// TODO Auto-generated method stub
		return subjectThreeResultMapper.selectSubjectThreeResultByFid(fid);
	}

	@Override
	public int submitSubjectThreeResult(SubjectThreeResult subjectThreeResult) {
		return subjectThreeResultMapper.submitSubjectThreeResult(subjectThreeResult);
	}

	@Override
	public int updateSubjectThreeResult(SubjectThreeResult subjectThreeResult) {
		// TODO Auto-generated method stub
		int row1 =0;
		int row2=0;
		//审核单据
		row1 =subjectThreeResultMapper.updateByPrimaryKeySelective(subjectThreeResult);
		subjectThreeResult.setSubjectThreeResultentryList(subjectThreeResultentryMapper.selSubjectThreeResultentryByParentId(subjectThreeResult.getFid(), null,null));
		List<SubjectThreeResultentry> subjectThreeResultentryList = subjectThreeResult.getSubjectThreeResultentryList();
		// 修改学员状态 科目三考试时间 和考试次数
		// 获取该学员的科目三考试次数
		// 获取当前修改时间
		String updateTime = TimeFormat.timeFormat(new Date());
		for (int i = 0; i < subjectThreeResultentryList.size(); i++) {
			Student _student = studentMapper.selectStudentById(subjectThreeResultentryList.get(i).getCfstudentid());
			Student student = new Student();
			student.setCfkmskss(_student.getCfkmskss());
			student.setFid(subjectThreeResultentryList.get(i).getCfstudentid());
			if(student.getCfkmskss() == null){//如果等于null设置值为0
				student.setCfkmskss(0L);
			}
			if (student.getCfkmskss() < 10 || student.getCfkmskss() == null) {
				student.setCfkmskss(student.getCfkmskss() + 1L);// 次数加一
				if (subjectThreeResultentryList.get(i).getCfexamresult() == 1) {// 合格
					//student.setCfkmskss(_student.getCfkmskss()+1L);//科目三考试次数加一
					student.setCfkmshgrq(TimeFormat.timeFormat(new Date()));//科目三通过时间
					if(_student.getCfkmehgrq() !=null){//先判断科目二通过时间是否存在 
						student.setCfstudentstate(7L);//科目三通过 科目二通过									
					}else if(_student.getCfkmebjgsj() !=null || _student.getCfkmeksqksj()!=null){//判断该学员的未通过科目二时间 和缺考时间									
						student.setCfstudentstate(17L);//科目三通过 科目二未通过									
					}else{//如果全部都为空则说明该学员未开始过科目三
						student.setCfstudentstate(19L);//科目三通过 科目二未通过									
					}
					
				} else if (subjectThreeResultentryList.get(i).getCfexamresult() == 2) {// 不合格
					//student.setCfkmskss(_student.getCfkmskss()+1L);//科目三考试次数加一
					student.setCfkmsbjgsj(TimeFormat.timeFormat(new Date()));//科目三未通过时间
					if(_student.getCfkmehgrq() !=null){//先判断科目二通过时间是否存在 
						student.setCfstudentstate(8L);//科目三未通过 科目二通过									
					}else if(_student.getCfkmebjgsj() !=null || _student.getCfkmeksqksj()!=null){//判断该学员的未通过科目二时间 和缺考时间									
						student.setCfstudentstate(20L);//科目三未通过 科目二未通过									
					}else{//如果全部都为空则说明该学员未开始过科目二
						student.setCfstudentstate(21L);//科目三未通过 科目二未开始								
					}
				} else {// 缺考
					//student.setCfkmekss(_student.getCfkmekss()+1L);//科目二考试次数加一
					student.setCfkmsbjgsj(TimeFormat.timeFormat(new Date()));//科目三未通过时间
					student.setCfkmsksqksj(TimeFormat.timeFormat(new Date()));//科目三缺考时间
					student.setCfkmsqkcs((_student.getCfkmsqkcs() == null ? 0:_student.getCfkmsqkcs()) +1L);
					if(_student.getCfkmehgrq() !=null){//先判断科目二通过时间是否存在 
						student.setCfstudentstate(8L);//科目三未通过 科目二通过									
					}else if(_student.getCfkmebjgsj() !=null || _student.getCfkmeksqksj()!=null){//判断该学员的未通过科目二时间 和缺考时间									
						student.setCfstudentstate(20L);//科目三未通过 科目二未通过									
					}else{//如果全部都为空则说明该学员未开始过科目二
						student.setCfstudentstate(21L);//科目三未通过 科目二未开始								
					}
				}
				// 修改
				row2+=studentMapper.updateStudent(student);
			} /*else if (student.getCfkmskss() == 5) {// 如果考试次数等于5则需要重新交钱考试
													// 把学生状态设置为作废
				// 设置学员状态 14为作废
				student.setCfstudentstate(14L);
				student.setCfkmsbjgsj(updateTime);// 设置科三不及格时间
				// 修改
				row2 +=studentMapper.updateStudent(student);
			}*/
		}
		if(row1 >0 && row2>0){
			return 1;
		}
		return 0;
	}

	@Override
	public int updateSubjectThreeResult(User user,SubjectThreeResult subjectThreeResult, String[] stuid, String[] stufseq,
			String[] stucfzkzmbh, String[] stufnumber, String[] stuname, String[] stucfidcardno, String[] stucfsex,
			String[] stucfsjhm, String[] stucflxdh, String[] stucfslzjcxdh, String[] stucfclazzName,
			String[] stucfamount, String[] stucfreceived, String[] stucfarrearage, String[] stucffavorable,
			String[] stucfvip, String[] stucflocal, String[] stucfexamresult, String[] stucfremark) {
		// TODO Auto-generated method stub
		//修改父类
		int row1 = subjectThreeResultMapper.updateByPrimaryKeySelective(subjectThreeResult);
		//删除子类
		subjectThreeResultentryMapper.deleteByParentId(subjectThreeResult.getFid());
		// 添加子类
		int row2=0;
		for (int i = 0; i < stuid.length; i++) {
			SubjectThreeResultentry subjectThreeResultentry = new SubjectThreeResultentry();
			subjectThreeResultentry.setFseq(Long.valueOf(stufseq[i]));
			subjectThreeResultentry.setCfzkzmbh(stucfzkzmbh[i]);
			subjectThreeResultentry.setFid(GetFid.UUIDIntoFid());
			subjectThreeResultentry.setFparentid(subjectThreeResult.getFid());
			subjectThreeResultentry.setCfstudentid(stuid[i]);
			subjectThreeResultentry.setStudentName(stuname[i]);
			subjectThreeResultentry.setCfstudentno(stufnumber[i]);
			subjectThreeResultentry.setCfidcardno(stucfidcardno[i]);
			subjectThreeResultentry.setCfsex(Long.valueOf(stucfsex[i]));
			subjectThreeResultentry.setCfmobile(stucfsjhm[i]);
			subjectThreeResultentry.setCfphone(stucflxdh[i]);
			subjectThreeResultentry.setCfsqzjcx(stucfslzjcxdh[i]);
			subjectThreeResultentry.setCfclazz(stucfclazzName[i]);
			subjectThreeResultentry.setCfreceivable(new BigDecimal(stucfamount[i]));
			subjectThreeResultentry.setCfreceived(new BigDecimal(stucfreceived[i]));
			subjectThreeResultentry.setCfarrearage(new BigDecimal(stucfarrearage[i]));
			subjectThreeResultentry.setCffavorable(new BigDecimal(stucffavorable[i]));
			subjectThreeResultentry.setCfvip(Long.valueOf(stucfvip[i]));
			subjectThreeResultentry.setCfbdyd(Long.valueOf(stucflocal[i]));
			subjectThreeResultentry.setCfexamresult(Long.valueOf(stucfexamresult[i]));
			subjectThreeResultentry.setCfremark(stucfremark[i]);
			row2 +=subjectThreeResultentryMapper.insertSelective(subjectThreeResultentry);
			// 添加日志
			customLogService.addLog("考试管理", "修改了学员:"+subjectThreeResultentry.getCfstudentno()+subjectThreeResultentry.getStudentName()
					+",科目三考试结果登记!申请车型:"+subjectThreeResultentry.getCfsqzjcx()+",考试日期:"+subjectThreeResult.getFbizdate()+",单据编号:"+subjectThreeResult.getFnumber(), subjectThreeResultentry.getCfstudentid(), user.getFid());
		}
		if(row1 >0 && row2>0){
			return 1;
		}
		return 0;
	}

	@Override
	public List<SubjectThreeResult> selectSubjectThreeResult(String companyId, String fnumber, String fdescription, String beginTime, String endTime) {
		Map<String,String> map =new HashMap<String,String>();
		map.put("companyId",companyId);
		map.put("fnumber",fnumber);
		map.put("fdescription",fdescription);
		map.put("beginTime",beginTime);
		map.put("endTime",endTime);
		return subjectThreeResultMapper.selectSubjectThreeResult(map);
	}

}
